package com.gu.controller;

import com.github.pagehelper.PageInfo;
import com.gu.VO.LayuiVO;
import com.gu.VO.ServerResponse;
import com.gu.constant.Constant;
import com.gu.entity.ApartmentDO;
import com.gu.service.ApartmentService;
import com.gu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.controller
 * @ClassName: ApartmentController
 * @Author: gusongliao
 * @Description: 公寓控制层
 * @createDate 2021/01/27 16:18
 */
@Controller
@RequestMapping("/apartment")
public class ApartmentController {

    @Autowired
    private ApartmentService apartmentService;

    @Autowired
    private UserService userService;

    /**
    * @Description //显示入住/未入住的房间
    * @User 谷松辽
    * @Date 2021/2/9 15:41
    * @param apartmentDO
    * @param model
    * @return java.lang.String
    */
    @RequestMapping("/queryApartment")
    public String queryApartment(ApartmentDO apartmentDO, Model model){
        //将楼层号放入ModelMap
        model.addAttribute("buildingNumberList",Constant.BUILDING_NUMBER_LIST);
        //将单元号放入ModelMap
        model.addAttribute("unitNumberList",Constant.UNIT_NUMBER_LIST);
        //将房间号放入ModelMap
        model.addAttribute("floorNumberList",Constant.FLOOR_NUMBER_LIST);
        //查询未入住房间
//        apartmentService.getNotUsedApartmentRoomNumber(apartmentDO);
        return "addOwner";
    }



    @RequestMapping("/listApartment")
    @ResponseBody
    public LayuiVO listApartment(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,ApartmentDO apartmentDO) {
        PageInfo apartmentDOList = apartmentService.listApartment(pageNum,pageSize,apartmentDO);
        return LayuiVO.success(apartmentDOList.getTotal(),apartmentDOList.getList());
    }


    @PostMapping("/updateApartment")
    @ResponseBody
    public ServerResponse updateApartment(@RequestBody ApartmentDO apartmentDO){
        int i = apartmentService.updateApartment(apartmentDO.getUserId(),apartmentDO.getId());

        if (i == 1){
            return ServerResponse.success(i);
        }
        return ServerResponse.error(3,"更新公寓业主数据不正确");
    }

    @PostMapping("/updateApartmentUserId")
    @ResponseBody
    public ServerResponse updateApartmentUserId(@RequestBody ApartmentDO apartmentDO){
        int i = apartmentService.updateApartmentUserId(apartmentDO.getUserId(),apartmentDO.getId());

        if (i == 1){
            return ServerResponse.success(i);
        }
        return ServerResponse.error(4,"删除公寓业主数据不正确");
    }

    @RequestMapping("/repair")
    public String repair(){
        System.out.println("repair");

        return "indexRepair";
    }


}
